//
//  BossViewController.m
//  JinChengYun
//
//  Created by 金橙云 on 2017/3/8.
//  Copyright © 2017年 金橙云. All rights reserved.
//

#import "BossViewController.h"

#import "ViewController.h"
#import "ZiViewController.h"//个人资料
#import "MyLetterViewController.h"//我的站内信
#import "CeoJiaGeVC.h"
#import "CEOYouHuiVC.h"
#import "CEOTeBieVC.h"

#import "PeopleOneCell.h"
#import "PeopleTwoCell.h"

#import "TodoHttpManager.h"

#import "CeoYouShenPiVC.h"//修改后的
@interface BossViewController ()<UITableViewDelegate,UITableViewDataSource>

{
    NSString *uid;
    NSArray *_imgArr;
    NSArray *_titArr;
    NSNumber *_countM;
//    NSNumber *_teBieM;
//    NSNumber *_jiaGeM;
//    NSNumber *_youHuiM;
    
}
@property (nonatomic,copy) NSString *teBieM;
@property (nonatomic,copy) NSString *jiaGeM;
@property (nonatomic,copy) NSString *youHuiM;

@property(nonatomic,strong)UITableView *tabView;
@property(nonatomic,strong)NSMutableDictionary *peopleDic;
@property(nonatomic,strong)NSMutableArray *hongDianArr;
@property(nonatomic,strong)NSMutableDictionary *hongDic;





@end

@implementation BossViewController


-(void)viewWillAppear:(BOOL)animated
{
    
    NSUserDefaults *userDefa = [NSUserDefaults standardUserDefaults];
    
    uid= [userDefa objectForKey:@"USER_ID"];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didTDGetMineGetInfo:) name:TDGetMineGetInfo object:nil];
    [_manager userGetMineInfoGetWithID:uid];
    
    
    //小红点
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didTDCeoMessage:) name:TDCeoMessage object:nil];
    //消息提醒
    [_manager UserCeoMessage];
}


-(void)didTDGetMineGetInfo:(NSNotification *)tific
{
    if (tific.object != nil)
    {
        _peopleDic = [tific.object objectForKey:@"data"];
        // NSLog( @"里边的跟人信息--peopleDic-0--%@",_peopleDic);
        
      
        
        [_tabView reloadData];
        
        
        
        
    }
}

//小红点
-(void)didTDCeoMessage:(NSNotification *)tif
{
    if (tif.object != nil)
    {
        _countM = [tif.object objectForKey:@"count"];
        
        _teBieM = [NSString stringWithFormat:@"%@",[tif.object objectForKey:@"tbyh"]];
        _jiaGeM = [NSString stringWithFormat:@"%@",[tif.object objectForKey:@"jgsp"]];
        _youHuiM = [NSString stringWithFormat:@"%@",[tif.object objectForKey:@"yhzc"]];
        self.hongDianArr = [NSMutableArray arrayWithCapacity:0];
        
//        [self.hongDianArr addObject:_countM];
//         [self.hongDianArr addObject:_teBieM];
//         [self.hongDianArr addObject:_jiaGeM];
//         [self.hongDianArr addObject:_youHuiM];
        
//        self.hongDic = [NSMutableDictionary dictionaryWithCapacity:0];
//        [self.hongDic setObject:self.hongDianArr forKey:@"hong"];
        NSLog(@"--------这个数组里有什么--------%@",self.hongDic);
        
        [_tabView reloadData];
    }
}


-(void)dealloc
{
    [[NSNotificationCenter defaultCenter]removeObserver:self name:TDGetMineGetInfo object:nil];
    [[NSNotificationCenter defaultCenter]removeObserver:self name:TDCeoMessage object:nil];
}


- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"个人中心";
    self.view.backgroundColor = BgColor;

    _manager = [TodoHttpManager sharedManager];
    _peopleDic = [NSMutableDictionary dictionary];
    _titArr = @[@"",@"     我的站内信",@"    价格审批",@"    优惠政策审批",@"    特别优惠审批"];
   _imgArr = @[@"",@"znx",@"xiaoshouMoney",@"ge_hui",@"me_te"];


    _hongDianArr = [NSMutableArray array];
    _hongDic = [NSMutableDictionary dictionary];
    _tabView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, B_WIDTH, B_HEIGHT) style:UITableViewStyleGrouped];
    _tabView.delegate = self;
    _tabView.dataSource = self;
    
    _tabView.backgroundColor = BgColor;
    [_tabView registerClass:[PeopleOneCell class] forCellReuseIdentifier:@"CellE"];
    [_tabView registerClass:[PeopleTwoCell class] forCellReuseIdentifier:@"CellA"];
    
    [self.view addSubview:_tabView];
    
    _tabView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(hongDianRefresh)];


}

-(void)hongDianRefresh
{
    //消息提醒
    [_manager UserCeoMessage];
    [_tabView.mj_header endRefreshing];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 5;
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    return nil;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0)
    {
        NSString *IDCell = @"Cell";
        PeopleOneCell *cell = [tableView dequeueReusableCellWithIdentifier:IDCell];
        if (cell == nil)
        {
            cell = [[PeopleOneCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:IDCell];
        }
        
        if ([cell isKindOfClass:[PeopleOneCell class]])
        {
            if (_peopleDic != nil)
            {
                [(PeopleOneCell *)cell setupPeopleCellWithHeaderImage:[_peopleDic objectForKey:@"img"] withNickName:[_peopleDic objectForKey:@"name"]];
            }
            [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
        }
        return cell;
    }
    else
    {
        
        NSString *CellIdentifier1 = [NSString stringWithFormat:@"Cell%ld%ld", (long)[indexPath section], (long)[indexPath row]];
        PeopleTwoCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier1];
        if (cell==nil)
        {
            
            cell = [[PeopleTwoCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier1];
        }

        
        cell.tuImg.image = [UIImage imageNamed:_imgArr[indexPath.row]];
        cell.titleLab.text = _titArr[indexPath.row];
        //_teBieM
//        _jiaGeM
//        _youHuiM
         NSLog(@"%@ ---- %@ ----- %@",_teBieM,_jiaGeM,_youHuiM);
        if ([_jiaGeM isKindOfClass:[NSNull class]])
        {
            cell.hongdainImg.backgroundColor = [UIColor whiteColor];
        }
        else
        {
            if (indexPath.row == 2) {
                if ([_jiaGeM isEqualToString:@"0"]) {
                    cell.hongdainImg.backgroundColor = [UIColor whiteColor];
                }else{
                    cell.hongdainImg.backgroundColor = [UIColor redColor];
                }
            }else if (indexPath.row == 3){
                if ([_youHuiM isEqualToString:@"0"]) {
                    cell.hongdainImg.backgroundColor = [UIColor whiteColor];
                }else{
                    cell.hongdainImg.backgroundColor = [UIColor redColor];
                }
                
            }else if (indexPath.row == 4){
                if ([_teBieM isEqualToString:@"0"]) {
                    cell.hongdainImg.backgroundColor = [UIColor whiteColor];
                }else{
                    cell.hongdainImg.backgroundColor = [UIColor redColor];
                }
                
            }
        }
        

    
        
        
        [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
        return cell;
        
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0)
    {
        return 80;
    }
    else
    {
        return 50;
    }
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 2;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 100;
}


-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, B_WIDTH, 100)];
    view.backgroundColor = BgColor;
    [self.view addSubview:view];
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame = CGRectMake(20, 30, B_WIDTH-20*2, 40);
    btn.layer.cornerRadius = 5;
    btn.backgroundColor = [UIColor orangeColor];
    [btn setTitle:@"退出" forState:0];
    [btn setTitleColor:[UIColor whiteColor] forState:0];
    btn.titleLabel.font = [UIFont systemFontOfSize:16];
    [btn addTarget:self action:@selector(ceoTuiBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [view addSubview:btn];
    return view;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    switch (indexPath.row)
    {
        case 0:{
            ZiViewController *mainVC = [[ZiViewController alloc] init];
            [self.navigationController pushViewController:mainVC animated:NO];
        }
            
            break;
        case 1:{
            MyLetterViewController *xinVC = [[MyLetterViewController alloc] init];
            [self.navigationController pushViewController:xinVC animated:NO];
            
        }
            
            break;
        case 2:{
            CeoJiaGeVC *huiVC = [[CeoJiaGeVC alloc] init];
            [self.navigationController pushViewController:huiVC animated:NO];
            
        }
            
            break;
        case 3:{
            CeoYouShenPiVC *jinVc = [[CeoYouShenPiVC alloc] init];
            [self.navigationController pushViewController:jinVc animated:NO];
            
        }
            
            break;
        case 4:{
            CEOTeBieVC *jiVC = [[CEOTeBieVC alloc] init];
            [self.navigationController pushViewController:jiVC animated:NO];
            
        }
            
            break;
        default:
            break;
    }

}


-(void)ceoTuiBtnClick:(UIButton *)btn
{
    //切换到主线程执行
    [self performSelectorOnMainThread:@selector(showExitAlert) withObject:self waitUntilDone:NO];
    

}

-(void)showExitAlert
{
    UIAlertController* alert = [UIAlertController alertControllerWithTitle:@"确定要退出?" message:nil preferredStyle:UIAlertControllerStyleAlert];
    
    [alert addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        
    }]];
    
    [alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"UID"];
        [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"USER_ID"];
        [[NSUserDefaults standardUserDefaults]removeObjectForKey:@"NetHeadImage"];
        [[NSUserDefaults standardUserDefaults]removeObjectForKey:@"LocalHeadImage"];
        [[NSUserDefaults standardUserDefaults]removeObjectForKey:@"STATE"];
        [[NSUserDefaults standardUserDefaults]removeObjectForKey:@"PROJECT_NAME"];
        [[NSUserDefaults standardUserDefaults]removeObjectForKey:@"LOGIN_NAME"];
        [[NSUserDefaults standardUserDefaults]removeObjectForKey:@"ONLY_URL"];
        [[NSUserDefaults standardUserDefaults]removeObjectForKey:@"BTG"];
        
        
        
        ViewController *viewCC = [[ViewController alloc] init];
        [self presentViewController:viewCC animated:NO completion:nil];
        
        
    }]];
    
    [alert.view setTintColor:[UIColor colorWithRed:37/255.0 green:85/255.0 blue:100/255.0 alpha:1]];
    [self.navigationController presentViewController:alert animated:YES completion:nil];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
